//
// Source code recreated from a .class file by IntelliJ IDEA
// (powered by FernFlower decompiler)
//

package com.ruoyi.system.service.impl;

import com.ruoyi.common.core.utils.DateUtils;
import com.ruoyi.common.security.service.TokenService;
import com.ruoyi.common.security.utils.SecurityUtils;
import com.ruoyi.system.api.domain.NoticeReadVo;
import com.ruoyi.system.api.domain.SwglAnnouncement;
import com.ruoyi.system.api.domain.SysUser;
import com.ruoyi.system.domain.vo.AnnounceVo;
import com.ruoyi.system.mapper.SwglAnnouncementMapper;
import com.ruoyi.system.mapper.SysUserMapper;
import com.ruoyi.system.service.ISwglAnnouncementService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
import java.util.UUID;
import java.util.stream.Collectors;

@Service
public class SwglAnnouncementServiceImpl implements ISwglAnnouncementService {
    @Autowired
    private SwglAnnouncementMapper swglAnnouncementMapper;
    @Autowired
    private TokenService tokenService;
    @Autowired
    private SysUserMapper userMapper;


    public SwglAnnouncement selectSwglAnnouncementByAnnouceId(String annouceId) {
        SwglAnnouncement swglAnnouncement = this.swglAnnouncementMapper.selectSwglAnnouncementByAnnouceId(annouceId);
        List<NoticeReadVo> noticeReadVos = this.swglAnnouncementMapper.selectIsReadUser(swglAnnouncement.getAnnouceId());
        swglAnnouncement.setNoticeReadVos(noticeReadVos);
        return swglAnnouncement;
    }

    public List<SwglAnnouncement> selectSwglAnnouncementList(SwglAnnouncement swglAnnouncement) {
        List<SwglAnnouncement> swglAnnouncements = this.swglAnnouncementMapper.selectSwglAnnouncementList(swglAnnouncement);
        Iterator var3 = swglAnnouncements.iterator();

        while(var3.hasNext()) {
            SwglAnnouncement announcement = (SwglAnnouncement)var3.next();
            List<NoticeReadVo> noticeReadVos = this.swglAnnouncementMapper.selectIsReadUser(announcement.getAnnouceId());
            announcement.setNoticeReadVos(noticeReadVos);
        }

        return swglAnnouncements;
    }

    public List<SwglAnnouncement> selectSwglAnnouncementShowList() {
        String orgNo = this.tokenService.getLoginUser().getSysUser().getOrgNo();
        List<SwglAnnouncement> swglAnnouncements = this.swglAnnouncementMapper.selectSwglAnnouncementShowList(orgNo);
        List<Long> userIds = this.swglAnnouncementMapper.selectRoleUsers();
        List<String> users = (List)userIds.stream().map((x) -> {
            return x + "";
        }).collect(Collectors.toList());
        List<SwglAnnouncement> rtnList = new ArrayList();
        Iterator var6 = swglAnnouncements.iterator();

        while(var6.hasNext()) {
            SwglAnnouncement swglAnnouncement = (SwglAnnouncement)var6.next();
            if (users.contains(swglAnnouncement.getCreateBy())) {
                rtnList.add(swglAnnouncement);
            }
        }

        return rtnList;
    }

    public AnnounceVo selectSwglAnnouncementListByType() {
        String orgNo = this.tokenService.getLoginUser().getSysUser().getOrgNo();
        List<SwglAnnouncement> list = this.swglAnnouncementMapper.selectSwglAnnouncementListByType(orgNo);
        AnnounceVo announceVo = new AnnounceVo();
        Iterator var4 = list.iterator();

        while(var4.hasNext()) {
            SwglAnnouncement swglAnnouncement = (SwglAnnouncement)var4.next();
            if (swglAnnouncement.getAnnouceType().equals("00")) {
                announceVo.setAnnounceNumber(swglAnnouncement.getNum());
            } else if (swglAnnouncement.getAnnouceType().equals("01")) {
                announceVo.setNoticewNumber(swglAnnouncement.getNum());
            } else if (swglAnnouncement.getAnnouceType().equals("02")) {
                announceVo.setInformationNumber(swglAnnouncement.getNum());
            } else {
                announceVo.setIdeaNumber(swglAnnouncement.getNum());
            }
        }

        return announceVo;
    }

    public AnnounceVo selectSwglAnnouncementListByEmergencyLevel() {
        AnnounceVo announceVo = new AnnounceVo();
        String orgNo = this.tokenService.getLoginUser().getSysUser().getOrgNo();
        announceVo.setYellowNumber(String.valueOf(this.swglAnnouncementMapper.selectSwglAnnouncementListByEmergencyLevel("02", orgNo)));
        announceVo.setRedNumber(String.valueOf(this.swglAnnouncementMapper.selectSwglAnnouncementListByEmergencyLevel("01", orgNo)));
        announceVo.setEmergencySum(String.valueOf(Integer.parseInt(announceVo.getRedNumber()) + Integer.parseInt(announceVo.getYellowNumber())));
        return announceVo;
    }

    public int insertSwglAnnouncement(SwglAnnouncement swglAnnouncement) {
        String uuid = UUID.randomUUID().toString().replaceAll("-", "");
        swglAnnouncement.setAnnouceId(uuid);
        Long userId = SecurityUtils.getUserId();
        SysUser sysUser = new SysUser();
        String orgNo = this.tokenService.getLoginUser().getSysUser().getOrgNo();
        sysUser.setOrgNo(orgNo);
        List<SysUser> sysUsers = this.userMapper.selectUserList(sysUser);
        Iterator var7 = sysUsers.iterator();

        while(var7.hasNext()) {
            SysUser user = (SysUser)var7.next();
            NoticeReadVo noticeReadVo = new NoticeReadVo();
            noticeReadVo.setAnnounceId(uuid);
            noticeReadVo.setUserName(user.getNickName());
            noticeReadVo.setUserId(user.getUserId());
            noticeReadVo.setIsRead("0");
            this.swglAnnouncementMapper.insertIsRead(noticeReadVo);
        }

        swglAnnouncement.setCreateBy(String.valueOf(userId));
        swglAnnouncement.setCreateTime(DateUtils.getNowDate());
        return this.swglAnnouncementMapper.insertSwglAnnouncement(swglAnnouncement);
    }

    public int updateSwglAnnouncement(SwglAnnouncement swglAnnouncement) {
        Long userId = SecurityUtils.getUserId();
        swglAnnouncement.setUpdateBy(String.valueOf(userId));
        swglAnnouncement.setUpdateTime(DateUtils.getNowDate());
        return this.swglAnnouncementMapper.updateSwglAnnouncement(swglAnnouncement);
    }

    public int deleteSwglAnnouncementByAnnouceIds(String[] annouceIds) {
        return this.swglAnnouncementMapper.deleteSwglAnnouncementByAnnouceIds(annouceIds);
    }

    public int deleteSwglAnnouncementByAnnouceId(String annouceId) {
        return this.swglAnnouncementMapper.deleteSwglAnnouncementByAnnouceId(annouceId);
    }

    public int selectIsReadUser(NoticeReadVo noticeReadVo) {
        return this.swglAnnouncementMapper.updateIsRead(noticeReadVo);
    }
}
